package com.marvels.financial.loan.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.dto.ReplacePaymentApplyInfo;
import com.marvels.financial.loan.dto.ReplacePaymentConditionDto;
import com.marvels.financial.loan.dto.SellBackCondition;
import com.marvels.financial.loan.service.ReplacePaymentApi;

/**
 * 代还款
 * @author tangpw
 *
 */
@RestController
@RequestMapping("financial/loan/replacePayment/service")
public class ReplacePaymentController {

	@Autowired
	private ReplacePaymentApi replacePaymentApi;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	
	
	/**
	 * 查询代还款申请数据
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("paymentApplyList")
	@ResponseBody
	public MarvelsResultDTO paymentApplyList(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			return replacePaymentApi.replacePaymentApplyInfo(replacePaymentConditionDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询代还款申请数据FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询代还款申请数据异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询需要审核的放款列表(处于放款申请状态)异常", null);
		}
	}

	/**
	 * 代还款申请
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("paymentApply")
	@ResponseBody
	public MarvelsResultDTO paymentApply(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			replacePaymentApi.replacePaymentApply(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					null);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>代还款申请处理FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>代还款申请处理异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "代还款申请处理异常", null);
		}
	}
	
	
	
	/**
	 * 审核通过列表
	 * @return
	 */
	@RequestMapping("replacePaymentAdoptList")
	@ResponseBody
	public MarvelsResultDTO replacePaymentAdoptList(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			Page<ReplacePaymentApplyInfo> list=replacePaymentApi.replacePaymentAdoptList(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询审核通过列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询审核通过列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询 审核通过列表异常", null);
		}
	}
	
	
	/**
	 * 审核拒绝列表
	 * @return
	 */
	@RequestMapping("replacePaymentRefuseList")
	@ResponseBody
	public MarvelsResultDTO replacePaymentRefuseList(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			Page<ReplacePaymentApplyInfo> list=replacePaymentApi.replacePaymentRefuseList(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询审核拒绝列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询审核拒绝列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询审核拒绝列表异常", null);
		}
	}
	
	
	/**
	 * 代还款审核列表
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("paymentAuditList")
	@ResponseBody
	public MarvelsResultDTO paymentAuditList(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			List<ReplacePaymentApplyInfo> list=replacePaymentApi.replacePaymentAuditList(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>代还款申请列表查询FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>代还款申请列表查询异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "代还款申请列表查询异常", null);
		}
	}
	
	
	
	
	
	/**
	 * 代还款审核通过
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("paymentAuditAdopt")
	@ResponseBody
	public MarvelsResultDTO paymentAuditAdopt(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			replacePaymentApi.replacePaymentAuditAdopt(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					null);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>代还款审核通过处理FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>代还款审核通过处理异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "代还款审核通过处理异常", null);
		}
	}
	
	
	
	/**
	 * 代还款审核拒绝
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("paymentAuditRefuse")
	@ResponseBody
	public MarvelsResultDTO paymentAuditRefuse(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			replacePaymentApi.replacePaymentAuditRefuse(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					null);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>代还款审核拒绝处理FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>代还款审核拒绝处理异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "代还款审核拒绝处理异常", null);
		}
	}
	
	
	
	
	
	/**
	 * 代还款查询
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("queryReplacePayment")
	@ResponseBody
	public MarvelsResultDTO queryReplacePayment(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			Map<String, Object> data = replacePaymentApi.queryPayment(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					data);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>代还款查询FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>代还款查询异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "代还款查询异常", null);
		}
	}
	
	
	
	
	
	/**
	 * 回销处理
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("sellBackDeal")
	@ResponseBody
	public MarvelsResultDTO sellBackDeal(@RequestBody SellBackCondition sellBackCondition) {
		try {
			replacePaymentApi.sellBackDeal(sellBackCondition);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					null);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>回销处理FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>回销处理异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "回销处理异常", null);
		}
	}
}
